﻿using BootstrapBlazor.Components;

namespace BPM.Web.Shared.Extensions;

/// <summary>
/// SelectedItem组件扩展
/// </summary>
public static class SelectedItemExtensions
{
    /// <summary>
    /// ITreeNode集合数据转换为CascaderItem
    /// </summary>
    /// <typeparam name="TKey">标识类型</typeparam>
    /// <param name="items"></param>
    /// <returns></returns>
    public static List<SelectedItem> BuildSelected<Tsource>(this IEnumerable<Tsource>? items, Action<Tsource, SelectedItem> action)
        where Tsource : class
    {
        if (items == null)
        {
            return new();
        }

        var result = new List<SelectedItem>();
        foreach (var item in items)
        {
            var node = new SelectedItem();
            action(item, node);
            result.Add(node);
        }
        return result;
    }
}
